﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class panelCarModel : UserControl
    {
        public panelCarModel()
        {
            InitializeComponent();
            this.gvCarInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvCarInfo.DataSource = LoadCar();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddCarModel frmAddCarModel = new frmAddCarModel();
            frmAddCarModel.ShowDialog();
            gvCarInfo.DataSource = LoadCar();
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                if (((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value != null && !((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value.ToString().Equals(string.Empty))
                {
                    frmEditCarModel frmEditCarModel = new frmEditCarModel(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()));
                    frmEditCarModel.ShowDialog();
                    gvCarInfo.DataSource = LoadCar();
                }
                else
                {
                    gvCarInfo.DataSource = LoadCar();
                }
            }
        }

        private void genGridview()
        {
            // gen Grideview
            gvCarInfo.AutoGenerateColumns = false;

            //Set Columns Count
            gvCarInfo.ColumnCount = 4;

            gvCarInfo.Columns[0].Width = 50;

            //Add Columns
            gvCarInfo.Columns[1].Name = "CAR_MODEL_ID";
            gvCarInfo.Columns[1].HeaderText = "รหัส";
            gvCarInfo.Columns[1].DataPropertyName = "CAR_MODEL_ID";
            gvCarInfo.Columns[1].Width = 60;

            gvCarInfo.Columns[2].Name = "CAR_BRAND_NAME";
            gvCarInfo.Columns[2].HeaderText = "ยี่ห้อรถยนต์";
            gvCarInfo.Columns[2].DataPropertyName = "CAR_BRAND_NAME";
            gvCarInfo.Columns[2].Width = 100;

            gvCarInfo.Columns[3].Name = "CAR_MODEL_NAME";
            gvCarInfo.Columns[3].HeaderText = "รุ่นรถยนต์";
            gvCarInfo.Columns[3].DataPropertyName = "CAR_MODEL_NAME";
            gvCarInfo.Columns[3].Width = 100;

            gvCarInfo.DataSource = LoadCar();
        }

        private DataTable LoadCar()
        {
            DataTable dt = DAL.CarModelInfoDAL.LoadAllCarModel(this.txtCarId.Text,this.txtModel.Text,this.txtBrand.Text);
            return dt;
        }
    }
}
